package com.zhl.spring.user;/**
 * <p>
 *
 * </p>
 *
 * @author zhanghaoliang
 * @Modified By:
 * @since 2021/1/22 18:04
 */

import java.sql.*;

/**
 * @author zhl
 * @date 2021/1/22 18:04
 */
public class test {
    public static void main(String[] args) {
        String sql = "insert into user2 values(?,?,?)";
        Connection conn = null;
        Statement st = null;
        try {
            // 1.加载注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            // 2.获取数据库连接
            conn = DriverManager.getConnection("jdbc:mysql:///test?characterEncoding=utf-8&useSSL=false","root","zhl1215.");
            // 3.创建语句对象
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setObject(1,10L);
            ps.setObject(2,"测试");
            ps.setObject(3,33);
            // 4.执行sql语句
            ps.executeUpdate();
            // 5.释放资源
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if (st!= null){
                    st.close();
                }
            } catch (SQLException e){
                e.printStackTrace();
            } finally {
                try {
                    if (conn != null){
                        conn.close();
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
